package org.stars.dao.sqlmapper.parser;

import static org.stars.config.Message.TEMPLATE2_PARSER_01;
import static org.stars.config.Message.TEMPLATE2_PARSER_02;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.stars.dao.sqlmapper.SqlBase;
import org.stars.dao.sqlmapper.SqlDefinition;
import org.stars.dao.sqlmapper.SqlMapper;
import org.stars.dao.sqlmapper.macro.MacroEvaluation;

public class TemplateParser2 {

	/**
	 * Parses the sqlMapper. It's invoked ONLY in load source time, so MacroEvaluation
	 * is fixed to SOURCE_PROCESSING.
	 *
	 * @param sqlMapper the sql mapper
	 * @throws Exception the exception
	 */
	public static void parse(SqlMapper sqlMapper) throws Exception
	{
		Log log=LogFactory.getLog(TemplateParser.class);

		log.debug(TEMPLATE2_PARSER_01.with(sqlMapper.getId()));
		
		for (SqlDefinition item: sqlMapper.getMap().values())
		{
			parse(MacroEvaluation.SOURCE_PROCESSING, item,item);
		}
		
		log.debug(TEMPLATE2_PARSER_02.with(sqlMapper.getId()));
	}
	
	/**
	 * Parses the sqlBase, component of sqlDefinition definition in both SOURCE_PROCESSING or
	 * RUNTIME_PROCESSING phase.
	 *
	 * @param when the when
	 * @param definition the definition
	 * @param item the item
	 * @throws Exception the exception
	 */
	public static void parse(MacroEvaluation when, SqlDefinition definition, SqlBase item) throws Exception 
	{
		SubParser05_MacroExecuter.execute(when, definition, item);
		SubParser06_FindParameters.execute(definition, item);			
	}		

}
